//
//  SearchViewController.swift
//  CompareNew
//
//  Created by 唐先才 on 16/1/27.
//  Copyright © 2016年 唐先才. All rights reserved.
//

import Foundation
import UIKit

class SearchViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
    
    var _button: UIButton?
    var _barView: UIView?
    var _willViewController: UIViewController?
    var _viewControllersArray: NSArray = []
    var _pvc: UIPageViewController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.whiteColor()
        self.createNav()
        self.createPageViewController()
    }
    
    func createNav() {
        let searchBar = UISearchBar(frame: CGRect(x: 60, y: 0, width: WIDTH - 60 - 100, height: 25))
        searchBar.placeholder = "搜一搜"
        guard let searchView = searchBar.subviews.last else {
            print("searchView生成失败")
            return
        }
        guard let searchField = searchView.subviews.last as? UITextField else {
            print("searchField生成失败")
            return
        }
        searchField.font = UIFont.systemFontOfSize(11)
        searchField.textColor = UIColor.grayColor()
        searchBar.layer.cornerRadius = 5
        searchBar.clipsToBounds = true
        searchBar.backgroundColor = UIColor.whiteColor()
        let titleView = UIView(frame: CGRect(x: 0, y: 0, width: WIDTH, height: 30))
        titleView.addSubview(searchBar)
        self.navigationItem.titleView = titleView
        
        let searchBtn = UIButton(type: .System)
        searchBtn.frame = CGRect(x: WIDTH - 80, y: 0, width: 60, height: 26)
        searchBtn.setTitle("搜索", forState: .Normal)
        searchBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        searchBtn.addTarget(self, action: "goToSearchPage", forControlEvents: .TouchUpInside)
        searchBtn.titleLabel?.font = UIFont.systemFontOfSize(16)
        titleView.addSubview(searchBtn)
    }
    
    func goToSearchPage() {
        
    }
    
    func createPageViewController() {
        //创建按钮
        let titleArray = ["分类", "商城"]
        for i in 0...1 {
            _button = UIButton(type: .System)
            guard let _button = _button else {
                print("创建按钮分屏按钮失败")
                return
            }
            _button.frame = CGRect(x: CGFloat(i) * WIDTH / 2, y: 0, width: WIDTH / 2, height: 28)
            _button.setTitle(titleArray[i], forState: .Normal)
            _button.setTitleColor(UIColor.grayColor(), forState: .Normal)
            if i == 0 {
                _button.setTitleColor(MAIN_COLOR, forState: .Normal)
            } else {
                _button.setTitleColor(UIColor.grayColor(), forState: .Normal)
            }
            guard let btnLab = _button.titleLabel else {
                print("设置按钮字体失败")
                return
            }
            btnLab.textAlignment = NSTextAlignment.Center
            btnLab.font = UIFont.systemFontOfSize(15)
            _button.tag = 100 + i
            _button.addTarget(self, action: "clickAction:", forControlEvents: .TouchUpInside)
            self.view.addSubview(_button)
        }
        
        //创建动画条
        _barView = UIView(frame: CGRect(x: WIDTH / 8, y: 28, width: WIDTH / 4, height: 2))
        guard let _barView = _barView else{
            return
        }
        _barView.backgroundColor = MAIN_COLOR
        self.view.addSubview(_barView)
        
        
        //创建PVC
        _pvc = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
        guard let _pvc = _pvc else {
            print("创建pvc失败")
            return
        }
        _pvc.view.backgroundColor = UIColor.grayColor()
        _pvc.view.frame = CGRect(x: 0, y: 30, width: WIDTH, height: HEIGHT - 30)
        _pvc.dataSource = self
        _pvc.delegate = self
        self.view.addSubview(_pvc.view)
        let category = CategoryViewController()
        category.view.backgroundColor = UIColor.whiteColor()
        let mall = MallViewController()
        mall.view.backgroundColor = UIColor.redColor()
        _viewControllersArray = [category, mall]
        _pvc.setViewControllers([category], direction: .Reverse, animated: false, completion: { (Bool) -> Void in
        })
    }
    
    func clickAction(button: UIButton) {
        guard let _pvc = _pvc, _barView = _barView else {
            return
        }
        guard let mallBtn = self.view.viewWithTag(101) as? UIButton else {
            return
        }
        guard let cateBtn = self.view.viewWithTag(100) as? UIButton else {
            return
        }
        cateBtn.setTitleColor(UIColor.grayColor(), forState: .Normal)
        mallBtn.setTitleColor(UIColor.grayColor(), forState: .Normal)
        if button.tag == 101 {
            UIView.animateWithDuration(0.3, animations: { () -> Void in
                _barView.frame = CGRect(x: CGFloat(5) * WIDTH / 8, y: 28, width: WIDTH / 4, height: 2)
            })
            button.setTitleColor(MAIN_COLOR, forState: .Normal)
            guard let _viewControllers1 = _viewControllersArray[1] as? UIViewController else {
                return
            }
            _pvc.setViewControllers([_viewControllers1], direction: .Reverse, animated: false, completion: { (Bool) -> Void in
            })
        } else if button.tag == 100 {
            UIView.animateWithDuration(0.3, animations: { () -> Void in
                _barView.frame = CGRect(x: WIDTH / 8, y: 28, width: WIDTH / 4, height: 2)
            })
            button.setTitleColor(MAIN_COLOR, forState: .Normal)
            guard let _viewControllers0 = _viewControllersArray[0] as? UIViewController else {
                return
            }
            _pvc.setViewControllers([_viewControllers0], direction: .Reverse, animated: false, completion: { (Bool) -> Void in
            })
        }
    }
    
    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        if viewController.isKindOfClass(CategoryViewController) {
            return nil
        }
        guard let _viewControllers0 = _viewControllersArray[0] as? UIViewController else {
            return nil
        }
        return _viewControllers0
    }
    
    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        if viewController.isKindOfClass(MallViewController){
            return nil
        }
        guard let _viewControllers1 = _viewControllersArray[1] as? UIViewController else {
            return nil
        }
        return _viewControllers1
    }
    
    func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        guard let _barView = _barView else {
            return
        }
        if completed {
            if let _willViewController = _willViewController {
                guard let mallBtn = self.view.viewWithTag(101) as? UIButton else {
                    return
                }
                guard let cateBtn = self.view.viewWithTag(100) as? UIButton else {
                    return
                }
                if _willViewController.isKindOfClass(MallViewController.self) {
                    mallBtn.setTitleColor(UIColor.grayColor(), forState: .Normal)
                    cateBtn.setTitleColor(UIColor.grayColor(), forState: .Normal)
                    UIView.animateWithDuration(0.3, animations: { () -> Void in
                        _barView.frame = CGRect(x: WIDTH * CGFloat(5) / 8, y: 28, width: WIDTH / 4, height: 2)
                    })
                    mallBtn.setTitleColor(MAIN_COLOR, forState: .Normal)
                } else {
                    mallBtn.setTitleColor(UIColor.grayColor(), forState: .Normal)
                    cateBtn.setTitleColor(UIColor.grayColor(), forState: .Normal)
                    UIView.animateWithDuration(0.3, animations: { () -> Void in
                        _barView.frame = CGRect(x: WIDTH / 8, y: 28, width: WIDTH / 4, height: 2)
                    })
                    cateBtn.setTitleColor(MAIN_COLOR, forState: .Normal)
                }
            }
        }
    }
    
    func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
        _willViewController = pendingViewControllers[0]
    }
}